Skip to content

Add MeshGL initializer taking per-triangle original IDs - #4

Merged
tomasf merged 1 commit into
tomasf:devfrom
fponticelli:meshgl-original-ids
Sep 11, 2026
Merged

tomasf merged 1 commit into
tomasf:devfrom
fponticelli:meshgl-original-ids

Conversation

@fponticelli

Copy link
Copy Markdown
Contributor

What

Adds a MeshGL initializer that takes one Manifold.OriginalID per triangle:

public init(vertices: [Vector], triangles: [Triangle], originalIDs: [Manifold.OriginalID])

Consecutive equal IDs are folded into runs, and runIndex / runOriginalID are populated accordingly. The existing two-argument initializer is unchanged; the shared setup moved into a private helper.

Why

runOriginalID is the field Manifold provides for exactly this, and its own documentation says so:

The OriginalID of the mesh this triangle run came from. This ID is ideal for reapplying materials to the output mesh. ... If you create an input MeshGL that you want to be able to reference as one or more originals, be sure to set unique values from ReserveIDs().

Today the Swift API can read those IDs but not write them. MeshGL.runs and MeshGL.originalIDs expose the output side, and Manifold.reserveOriginalIDs(_:) hands out IDs, but there is no way to attach them to an input mesh. Manifold.asOriginal() is all-or-nothing: it stamps one fresh ID across the whole solid. So a mesh whose faces have different provenance has to be built through the raw C++ API.

The case that prompted this is importing a 3MF file into Cadova. A 3MF mesh assigns a color or base material per triangle. To keep those materials attached to the right faces through later Boolean operations, each material's triangles need their own original ID at construction time. Nothing else in the Swift API can express that, and splitting the mesh per material is not an option, since the pieces are open surfaces rather than solids.

Behaviour worth knowing

Supplying IDs means the mesh states its own provenance, so Manifold does not additionally make it an original: Manifold.originalID returns nil even when every triangle carries the same ID. The IDs themselves are preserved and readable through meshGL().originalIDs. This mirrors the C++ behaviour, and one of the tests pins it so the distinction does not get lost.

API

let ids = Manifold.reserveOriginalIDs(2)
let mesh = MeshGL(vertices: vertices, triangles: triangles, originalIDs: perTriangleIDs)
let solid = try Manifold(mesh)
solid.meshGL().originalIDs   // [ids[0]: IndexSet, ids[1]: IndexSet]

The initializer preconditions on originalIDs.count == triangles.count. Runs need not be sorted by ID, and the same ID may appear in more than one run, which the tests cover.

Tests

Six tests in MeshGLOriginalIDsTests.swift, all on a small hand-built cube:

  • per-triangle IDs come back grouped by ID after Manifold construction
  • a mesh with several IDs is not itself an original, and asOriginal() collapses it onto one fresh ID
  • a single ID for every triangle is kept on the triangles without making the mesh an original
  • the same ID may appear in separate, non-contiguous runs
  • IDs survive a Boolean operation, alongside the cutter's own ID
  • an empty triangle list produces no runs

Full suite green locally on macOS: 87 tests.

Context

Needed by an in-progress change to Cadova that preserves colors and materials when importing 3MF models (tomasf/Cadova#2). That change cannot build until this lands and ships in a release, so I am opening this one first.

MeshGL could be built from vertices and triangles only, leaving runOriginalID
empty, so every mesh became a single anonymous original. Tagging subsets of a
mesh at construction time required dropping to the C++ API.

The new initializer takes one original ID per triangle and folds consecutive
equal IDs into runs. Manifold then reports those IDs through originalIDs, and
keeps them on the surviving faces through Boolean operations, which is what
runOriginalID exists for.
@tomasf
tomasf changed the base branch from main to dev September 11, 2026 21:58
@tomasf
tomasf merged commit 46d6b93 into tomasf:dev Sep 11, 2026
@tomasf

tomasf commented Sep 11, 2026

Copy link
Copy Markdown
Owner

Excellent, thanks!

@fponticelli
fponticelli deleted the meshgl-original-ids branch October 13, 2026 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants